package db_live

import (
	"errors"
	"fmt"
	"gorm.io/gorm"
	"longmen/server/config/global"
	"longmen/server/pkg/common/helper/util"
	"longmen/server/pkg/db/mysql"
	"longmen/server/pkg/db/mysql/models/models_live"
)

func GetLiveRoomManagerForIds(param map[string]interface{}, column string) ([]int, error) {
	fName := "GetLiveRoomManagerForIds"
	o := make([]int, 0)
	table := global.Live().Model(&models_live.LiveRoomManager{})
	mysql.Condition(table, param, "", "", 0, 0)
	err := table.Pluck(column, &o).Error
	if errors.Is(err, gorm.ErrRecordNotFound) {
		err = nil
	}
	if err != nil {
		err = fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return o, err
}

func GetLiveRoomManagerParam(param map[string]interface{}) (*models_live.LiveRoomManager, error) {
	fName := "GetLiveRoomManagerParam"
	o := &models_live.LiveRoomManager{}
	table := global.Live().Model(&models_live.LiveRoomManager{})
	mysql.Condition(table, param, "", "", 0, 0)
	err := table.Find(o).Error
	if errors.Is(err, gorm.ErrRecordNotFound) {
		err = nil
	}
	if err != nil {
		err = fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return o, err
}

func GetLiveRoomManagersParam(param map[string]interface{}, order string, page, size int) ([]*models_live.LiveRoomManager, error) {
	fName := "GetLiveRoomManagersParam"
	o := make([]*models_live.LiveRoomManager, 0)
	table := global.Live().Model(&models_live.LiveRoomManager{})
	mysql.Condition(table, param, "", order, page, size)
	err := table.Find(&o).Error
	if errors.Is(err, gorm.ErrRecordNotFound) {
		err = nil
	}
	if err != nil {
		err = fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return o, err
}

func GetLiveRoomManagerCount(param map[string]interface{}) (int64, error) {
	fName := "GetLiveRoomManagerCount"
	var o int64
	table := global.Live().Model(&models_live.LiveRoomManager{})
	mysql.Condition(table, param, "", "", 0, 0)
	err := table.Count(&o).Error
	if errors.Is(err, gorm.ErrRecordNotFound) {
		err = nil
	}
	if err != nil {
		err = fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return o, err
}

func InsertLiveRoomManager(manager *models_live.LiveRoomManager) error {
	fName := "InsertLiveRoomManager"
	err := global.Live().Create(manager).Error
	if err != nil {
		return fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return nil
}

func DeleteLiveRoomManager(param map[string]interface{}) error {
	fName := "DeleteLiveRoomManager"
	table := global.Live().Model(&models_live.LiveRoomManager{})
	mysql.Condition(table, param, "", "", 0, 0)
	err := table.Delete(&models_live.LiveRoomManager{}).Error
	if err != nil {
		return fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return nil
}
